package com.ghxxzx.dagl.controller;

import cn.gjing.tools.excel.ExcelFactory;
import com.ghxxzx.dagl.entity.*;
import com.ghxxzx.dagl.exceptions.JyException;
import com.ghxxzx.dagl.mapper.ClxxRgMapper;
import com.ghxxzx.dagl.service.DalrtwoService;
import com.ghxxzx.dagl.vo.ResultEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author: 刘华
 * @Date: 2020/10/12 9:42
 * @Version 1.0
 */
@Api(value = "档案录入")
@RestController
@RequestMapping("/dalr/api")
public class DalrControllertwo {
    Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    private ClxxRgMapper clxxRgMapper;
    @Autowired
    private DalrtwoService dalrtwoService;
    @RequestMapping("outClxxExcel")
    public ResultEntity outExcel(String dabh, HttpServletResponse response, HttpServletRequest request) {
        // 创建工作表
        response.reset();
        response.setCharacterEncoding("UTF-8");// 设置字符集
        try {
            List<ClxxRg> clxxRgs = clxxRgMapper.selectByDabh(dabh);
            if (clxxRgs==null||clxxRgs.size()<=0){
                return ResultEntity.error("没有可导出的信息");
            }
            ExcelFactory.createWriter(ClxxRg.class, response)
                    .write(clxxRgs)
                    .flush();
        } catch (JyException e) {
            log.error(e.getMess(), e);
            return ResultEntity.error(e.getMess());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultEntity.error("未知异常");
        }
        return null;
    }

    @ApiOperation(value = "获取录入时间", notes = "添加")
    @RequestMapping(value = "getinputtime")
    public ResultEntity getinputtime() {
        try {
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = sdf.format(date);
            return ResultEntity.ok(format);
        } catch (JyException e) {
            log.error(e.getMess(), e);
            return ResultEntity.error(e.getMess());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultEntity.error("未知异常");
        }
    }


    //待写
    @ApiOperation(value = "档案信息补入个人用房", notes = "添加")
    @RequestMapping(value = "dabrGryf")
    public ResultEntity dabrGryf(@RequestBody GryfKjRg gryfKjRg, HttpSession httpSession) {
        try {
            User user = (User) httpSession.getAttribute("user");
            return dalrtwoService.dabrGryf(gryfKjRg,user);
        } catch (JyException e) {
            log.error(e.getMess(), e);
            return ResultEntity.error(e.getMess());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultEntity.error("未知异常");
        }
    }

    @ApiOperation(value = "档案信息刪除个人用房", notes = "更新")
    @RequestMapping(value = "deletedaGryf")
    public ResultEntity deletedaGryf(Integer id) {
        try {
            return dalrtwoService.deletedaGryf(id);
        } catch (JyException e) {
            log.error(e.getMess(), e);
            return ResultEntity.error(e.getMess());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultEntity.error("未知异常");
        }
    }
    @ApiOperation(value = "档案信息更新个人用房", notes = "更新")
    @RequestMapping(value = "updatedaGryf")
    public ResultEntity updatedaGryf(@RequestBody GryfKjRg gryfKjRg) {
        try {
            return dalrtwoService.updatedaGryf(gryfKjRg);
        } catch (JyException e) {
            log.error(e.getMess(), e);
            return ResultEntity.error(e.getMess());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultEntity.error("未知异常");
        }
    }
//    @ApiOperation(value = "查询个人用房", notes = "更新")
//    @RequestMapping(value = "queryGryf")
//    public ResultEntity queryGryf(String dabh) {
//        try {
//            return dalrtwoService.queryGryf(dabh);
//        } catch (JyException e) {
//            log.error(e.getMess(), e);
//            return ResultEntity.error(e.getMess());
//        } catch (Exception e) {
//            log.error(e.getMessage(), e);
//            return ResultEntity.error("未知异常");
//        }
//    }
}
